package com.zbkj.business.kaoshi.task;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.annotation.OpenApiTag;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: exam_task卡任务列
 * Author: jason
 * @create: ${creatTime}
 **/
@Service("task")
@Slf4j
@OpenApi
//@DS("prod")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@OpenApiTag(value = "卡任务列",tag = "${projectName}")
public class TaskService extends BaseService<TaskDao, Task> implements IService<Task> {

    /**
     * list
     */
    @OpenApiTag("查询卡任务列列表")
    public Return list(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
//        cdoRequest.pageHandle();
//        List<CDO> pageList = this.baseMapper.getPageList(cdoRequest);
//        int total = this.baseMapper.getPageListCount(cdoRequest);
//        CDO cdoResult=new CDO().set("total",total).set("datalist",pageList);
        return super.getPageList(cdoRequest);
    }
    /**
     * info
     */
    @OpenApiTag("查询卡任务列信息")
    public Return info(CDO cdoRequest) {
        Task bo = BeanUtil.toBean(cdoRequest, Task.class);
        LambdaQueryWrapper<Task> lqw = buildQueryWrapper(bo);
        Task task=super.getOne(lqw);
        return Return.valueOf(task);
    }

    @OpenApiTag("根据ID查询卡任务列信息")
    public Return queryById(CDO cdoRequest) {
        Task task=super.getById(cdoRequest.getLongValue("id"));
        return Return.valueOf(task);
    }

    private LambdaQueryWrapper<Task> buildQueryWrapper(Task task) {
        LambdaQueryWrapper<Task> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Task::getId);

        lqw.eq(task.getId() != null, Task::getId, task.getId());
        lqw.like(StringUtils.isNotBlank(task.getTaskTitle()), Task::getTaskTitle, task.getTaskTitle());
        lqw.like(StringUtils.isNotBlank(task.getTaskCharge()), Task::getTaskCharge, task.getTaskCharge());
        lqw.like(StringUtils.isNotBlank(task.getTaskCommis()), Task::getTaskCommis, task.getTaskCommis());
        lqw.like(StringUtils.isNotBlank(task.getTaskUrl()), Task::getTaskUrl, task.getTaskUrl());
        lqw.like(StringUtils.isNotBlank(task.getTaskSign()), Task::getTaskSign, task.getTaskSign());
        lqw.like(StringUtils.isNotBlank(task.getTaskNotesa()), Task::getTaskNotesa, task.getTaskNotesa());
        lqw.like(StringUtils.isNotBlank(task.getTaskNotesb()), Task::getTaskNotesb, task.getTaskNotesb());
        lqw.like(StringUtils.isNotBlank(task.getTaskNotesc()), Task::getTaskNotesc, task.getTaskNotesc());
        lqw.like(StringUtils.isNotBlank(task.getCreatedAt()), Task::getCreatedAt, task.getCreatedAt());
        lqw.like(StringUtils.isNotBlank(task.getUpdatedAt()), Task::getUpdatedAt, task.getUpdatedAt());
        return lqw;
    }

    /**
     * save
     */
    @Override
    @OpenApiTag("保存卡任务列信息")
    public Return save(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Task bo = BeanUtil.toBean(cdoRequest, Task.class);
        Return ret = this.validEntity(cdoRequest,bo,0);
        if(ret.getCode()!=0){
            return ret;
        }
        super.save(bo);
        return Return.SUCCESS;
    }
    /**
     * update
     */
    @OpenApiTag("更新卡任务列信息")
    public Return update(CDO cdoRequest) {
        cdoRequest.remove("createtime");
        cdoRequest.remove("updatetime");
        Task bo = BeanUtil.toBean(cdoRequest, Task.class);
        Return ret = this.validEntity(cdoRequest,bo,1);
        if(ret.getCode()!=0){
            return ret;
        }
        super.updateById(bo);
        return Return.SUCCESS;
    }

    /**
     * delete
     */
    @OpenApiTag("删除卡任务列信息")
    public Return delete(CDO cdoRequest) {
        String[] ids = cdoRequest.getStringValue("ids").split(",");
        boolean b = super.removeBatchByIds(Arrays.asList(ids));
        if(b) {
            return Return.SUCCESS;
        }
        return Return.EXCEPTION;
    }

    private Return validEntity(CDO cdoRequest,Task entity,Integer type){
        //TODO 做一些数据校验,如唯一约束
        return Return.SUCCESS;
    }
}
